package com.lxl.bt.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lxl.bt.R;
import com.lxl.bt.activity.BaseFragment;
import com.lxl.bt.bean.CarAlarmBody;
import com.lxl.bt.bean.DevAlarmBody;
import com.lxl.bt.bean.NotifyBody;
import com.lxl.bt.bean.PerAlarmBody;
import com.lxl.bt.util.ACache;
import com.lxl.bt.util.EnvironmentUtil;
import com.lxl.bt.util.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Create by lvxinlong
 * on 消息
 * on 2020年5月9日15:32:57
 **/
public class MessageChildFragment extends BaseFragment {


    @BindView(R.id.rcv)
    RecyclerView rcv;
    @BindView(R.id.llNodata)
    LinearLayout llNodata;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private Unbinder unbinder;
    ACache aCache;
    BaseQuickAdapter<NotifyBody.OaNotifyListBean, BaseViewHolder> sysAdapter;
    BaseQuickAdapter<DevAlarmBody.DevAlarmListBean, BaseViewHolder> callAdapter;
    BaseQuickAdapter<CarAlarmBody.ListBean, BaseViewHolder> carAdapter;
    BaseQuickAdapter<PerAlarmBody.ListBean, BaseViewHolder> perAdapter;
    String type;
    List<String> promoItems = new ArrayList<>();
    int pageNo = 1;
    int parse = 0;

    public static MessageChildFragment newInstance(String type) {
        Bundle bundle = new Bundle();
        MessageChildFragment fragment = new MessageChildFragment();
        bundle.putString("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getString("type");

        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_messagechild, container, false);
//        ((BaseActivity) getActivity()).setStatusBarFullTransparent();

        //返回一个Unbinder值（进行解绑），注意这里的this不能使用getActivity()
        unbinder = ButterKnife.bind(this, view);
//        EventBus.getDefault().register(this);
        aCache = ACache.get(getActivity());

        iniSmartRefreshLayout();
        return view;
    }


    @Override
    public void onResume() {
        super.onResume();
    }

    private void iniSmartRefreshLayout() {
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                if (!EnvironmentUtil.isNetConnect(getActivity())) {
                    ToastUtil.showToast(getActivity(), "暂无网络");
                    refreshLayout.finishRefresh();
                    return;
                }
                parse=0;
                updateData();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                parse++;
                getData();
            }
        });

    }

    private void updateData() {
        pageNo = 1;
        getData();
    }

    @Override
    public void getData() {
    }




    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }


//    @Subscribe
//    public void onEventMainThread(EventTagBean event) {
//    }

    /**
     * onDestroyView中进行解绑操作
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

    }

}
